package service.local.beans;

import java.util.List;

import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Stateless;

import model.Candidate;
import model.Comment;
import model.Position;
import repository.PositionRepository;
import repository.exceptions.RepositoryException;
import service.ServiceException;
import service.local.PositionService;

@Stateless(name = "PositionService", mappedName = "PositionService")
@DependsOn({ "PositionRepository" })
public class PositionServiceBean implements PositionService {

	@EJB
	PositionRepository repo;

	@Override
	public Position insertPosition(Position Position) {
		Position r = new Position();
		try {
			return repo.save(Position);
		} catch (final RepositoryException ex) {
			// throw new ServiceException("Insert Position failed", ex);
		}
		return r;
	}

	@Override
	public Position updatePosition(Position Position){
		try {
			repo.merge(Position);
			return Position;
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	@Override
	public void deletePosition(Position Position){
		try {
			repo.delete(Position);
		} catch (RepositoryException e) {

		}
	}

	@Override
	public List<Position> getAll()  {
		try {
			return repo.getAll();
		} catch (final RepositoryException ex) {
			
		}
		return null;
	}

	public void addCandidateToPosition(Long positionID, Candidate c) {
		List<Candidate> can;
		try {
			can = repo.getById(positionID).getCandidates();
			can.add(c);
			repo.getById(positionID).setCandidates(can);
			repo.merge(repo.getById(positionID));
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public List<Candidate> getCandidatesForPosition(Long id)
			 {
		try {
			return repo.getCandidatesForPosition(id);
		} catch (final RepositoryException ex) {
			
		}
		return null;
	}

	@Override
	public List<Comment> getCommentsForPosition(Long id)
			 {
		try {
			return repo.getCommentsForPosition(id);
		} catch (final RepositoryException ex) {
			
		}
		return null;
	}

	@Override
	public List<Position> getActivePositions() {
		try {
			return repo.getActivePositions();
		} catch (final RepositoryException ex) {
			
		}
		return null;
	}

	@Override
	public List<Position> getInactivePositions(){
		try {
			return repo.getInactivePositions();
		} catch (final RepositoryException ex) {
		
		}
		return null;
	}

	@Override
	public void approvePosition(Long id){
		try {
			repo.approvePosition(id);
		} catch (final RepositoryException ex) {
			
		}

	}

	@Override
	public void deactivatePosition(Long id) {
		try {
			repo.deactivatePosition(id);
		} catch (final RepositoryException ex) {
		
		}

	}

	@Override
	public void selectCandidate(Long id, Candidate c){
		try {
			repo.selectCandidate(id, c);
		} catch (final RepositoryException ex) {
			
		}

	}

	@Override
	public void addCommentToPosition(Long id, Comment c)
			{
		try {
			repo.addCommentToPosition(id, c);
		} catch (final RepositoryException ex) {
			
		}

	}

	@Override
	public List<Position> getApprovedPositions() {
		try {
			return this.repo.getApprovedPositions();
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Position> getUnApprovedPositions() {
		try {
			return this.repo.getUnApprovedPositions();
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Position getById(Long id) {
		try {
			return this.repo.getById(id);
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Position getPositionByName(String position) {
		try {
			return this.repo.getPositionByName(position);
		} catch (RepositoryException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
